package com.ln.de1110.demo03;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author Ln
 * @version 终极版
 * @date 2022/11/10 16:36
 */
public class Test {
    public static void main(String[] args) throws SQLException {
        Connection connection=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","zsf@123");

            connection.setAutoCommit(false);

            String sql="update tab_user set money=money-500 where name='王德法'";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.executeUpdate();

            String sql1="update tab_user set money=money+500 where name='张三'";
            PreparedStatement preparedStatement1 = connection.prepareStatement(sql1);
            preparedStatement1.executeUpdate();
            connection.commit();
            System.out.println("转账成功");


        } catch (Exception e) {
            //事务回滚
            connection.rollback();
            e.printStackTrace();
        }
    }
}
